package com.swiftride.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swiftride.constants.Constants;
import com.swiftride.constants.GlobalExceptionCode;
import com.swiftride.exception.GlobalException;
import com.swiftride.feign.CommonFeign;
import com.swiftride.mapper.DriverContractMapper;
import com.swiftride.pojo.domain.Driver;
import com.swiftride.pojo.domain.DriverContract;
import com.swiftride.pojo.dto.DriverContractDto;
import com.swiftride.pojo.dto.LoginInfoDto;
import com.swiftride.result.R;
import com.swiftride.service.IDriverContractService;
import com.swiftride.service.IDriverService;
import com.swiftride.utils.AssertUtil;
import com.swiftride.utils.BitStatesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 司机合同表 服务实现类
 * </p>
 *
 * @author wy
 * @since 2024-08-12
 */
@Service
public class DriverContractServiceImpl extends ServiceImpl<DriverContractMapper, DriverContract> implements IDriverContractService {

    @Autowired
    private CommonFeign commonFeign;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private IDriverService driverService;

    @Override
    @Transactional
    public void upload(DriverContractDto driverContractDto, MultipartFile file) {
        //1.参数校验
        //2.业务校验
        //2.1 获取司机对象,司机对象不能为空
        Long driverId = driverContractDto.getDriverId();
        Driver driver = driverService.getById(driverId);
        AssertUtil.isNotNull(driver, GlobalExceptionCode.PARAM_PHONE_NOT_EXIST);
        //2.2 司机位状态中必须包含已实名认证状态
        Long bitState = driver.getBitState();
        if (!BitStatesUtil.hasState(bitState, BitStatesUtil.OP_REAL_AUTHENTICATED)) {
            throw new GlobalException(GlobalExceptionCode.PARAM_ERROR);
        }
        //2.3 司机位状态中不能包含已签署状态
        if (BitStatesUtil.hasState(bitState, BitStatesUtil.OP_DRIVER_AGREEMENT)) {
            throw new GlobalException(GlobalExceptionCode.PARAM_ERROR);
        }
        //2.4 查询上传人,上传人必须存在
        String loginIdKey = String.format(Constants.Redis.LOGIN_REDIS_KEY, StpUtil.getLoginIdAsString());
        LoginInfoDto loginInfoDto = (LoginInfoDto) redisTemplate.opsForValue().get(loginIdKey);
        AssertUtil.isNotNull(loginInfoDto, GlobalExceptionCode.DATA_STALENESS);

        //3.业务实现
        //3.1将文件通过feign接口发送给公共服务,获取图片存储地址
        R<String> uploadResult = commonFeign.serviceUploadFile("driverContract", file);
        //返回结果失败,抛出自定义异常
        if (!uploadResult.isSuccess()) {
            throw new GlobalException(GlobalExceptionCode.SERVICE_REQUEST_ERROR);
        }
        //文件路径
        String filePath = uploadResult.getData();
        //3.2保存合同表
        DriverContract driverContract = new DriverContract();
        driverContract.init();
        driverContract.setId(driverId);
        driverContract.setName(driverContractDto.getDriverName());
        driverContract.setNumber(driverContractDto.getNumber());
        driverContract.setFilePath(filePath);
        driverContract.setUploadName(loginInfoDto.getName());
        save(driverContract);
        //3.3修改司机位状态
        bitState = BitStatesUtil.addState(bitState, BitStatesUtil.OP_DRIVER_AGREEMENT);
        driver.setBitState(bitState);
        driverService.updateById(driver);
    }

    @Override
    public void cancel(Long contractId) {
        //1.参数校验
        //2.业务校验 - contractId和driverId相同
        //2.1 获取司机合同对象,司机合同对象不能为空
        DriverContract driverContract = getById(contractId);
        AssertUtil.isNotNull(driverContract, GlobalExceptionCode.SERVICE_ERROR);
        //2.2 司机合同状态必须为已签署状态
        //@TODO 消除魔法值
        Integer contractStatus = driverContract.getContractStatus();
        AssertUtil.isEquals(contractStatus, 1, GlobalExceptionCode.SERVICE_ERROR);
        //2.3 获取司机对象,司机对象不能为空
        Driver driver = driverService.getById(contractId);
        AssertUtil.isNotNull(driver, GlobalExceptionCode.PARAM_PHONE_NOT_EXIST);
        //2.4 司机位状态中不能包含未签署状态
        Long bitState = driver.getBitState();
        if (BitStatesUtil.hasState(bitState, BitStatesUtil.OP_DRIVER_AGREEMENT)) {
            throw new GlobalException(GlobalExceptionCode.SERVICE_ERROR);
        }

        //3.业务实现
        //3.1更新司机合同状态
        //@TODO 消除魔法值
        driverContract.setContractStatus(1);
        updateById(driverContract);
        //3.2更新司机位状态
        bitState = BitStatesUtil.removeState(bitState, BitStatesUtil.OP_DRIVER_AGREEMENT);
        driver.setBitState(bitState);
        driverService.updateById(driver);
    }

}
